home *** CD-ROM | disk | FTP | other *** search
- Path: apccorp.apcc.com!root
- From: nfegan@apcc.com (Noel Fegan)
- Newsgroups: comp.lang.c++
- Subject: Re: Help with bizzare bug (Long)
- Date: Thu, 29 Feb 1996 10:00:37 GMT
- Organization: American Power Conversion
- Message-ID: <4h3the$5bb@apccorp.apcc.com>
- References: <tday-2702962256130001@tday.slip.netcom.com> <4h19bq$m3m@apccorp.apcc.com>
- NNTP-Posting-Host: hewie.galway.apcc.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- nfegan@apcc.com (Noel Fegan) wrote:
-
- >tday@netcom.com (Tony Day) wrote:
-
- >>
- >>NameAr::NameAr()
- >>{
- >> name = new char[strlen("Smiley")+1];
- >> name = "Smiley";
- >>}
-
- >>
- >>NameAr::NameAr(const ArrayDb & a): ArrayDb(a)
- >>{
- >> name = new char[strlen("Smiley")+1];
- >> name = "Smiley";
-
- >>}
-
- I just looked at you constructors again. I failed to notice that you had in fact
- called new correctly to allocate a block of memory for the string, and correctly
- adding 1 byte for the '\0' character. Your mistake is an easy one to make, and
- is on the next line.
-
- name = "Smiley"
-
- This line sets the value of the pointer name to now point to the character array
- "Smiley" and does not copy the array to the memory block currently pointed to by
- 'name'. This means that the memory block allocate in the previous line becomes
- lost and is in effect a memory leak because you have lost you pointer to it, you
- no longer have any way of knowing where the block is and it will therefore not
- be deleted.
-
- You should have used the function strcpy. The line would then read:
-
- strcpy(name, "Smiley");
-
-
-
- Don't worry easy mistake to make...
- --
- Noel Fegan
- American Power Conversion
-
-